﻿using BaiTu.Common.Web.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BaiTu.UniqueIdGenerator
{
    public class IDGenerator
    {
        static IDGenerator()
        {
            string ipAddress = IPHelper.GetLocalIPv4();
            ip = IDGeneratorHelper.IP2Long(ipAddress);
        }

        private static long ip = 0;
        public static string Next()
        {
            return string.Format("{0}{1}{2}", DateTime.Now.ToString("yyyyMMddHHmmssff"), ip.ToString(), GetSequence().ToString());
        }

        private static int curSeq = 1;
        private static object o = 1;
        private static int GetSequence()
        {
            lock (o)
            {
                try
                {
                    if (curSeq > 999)
                        curSeq = 1;
                }
                catch
                {
                    curSeq = 0;
                }
                return curSeq++;
            }
        }
    }
}
